package com.linzelin.www.po;

import com.linzelin.www.util.Activity;
import com.linzelin.www.util.JDBCUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;
import java.util.Scanner;

public class Account_activity {
    /*Properties prop = new Properties();*/
    private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());
    //先获取一波连接池

   /* public boolean login(String activity) {
        if (activity == null) {
            return false;
        }
        String sql = "select * from activity";
        //获取表中符合条件的活动,有就返回true,没有就返回false
        List<Activity> list = template.query(sql,new BeanPropertyRowMapper<Activity>(Activity.class));
        for (Activity a : list) {
            if(a.getActivity().equals(activity))
            {
                return true;
            }
        }
        return false;
    }*/
    public void Activity_add(Activity activity)  {
        System.out.println("请输入活动内容");
        activity.setStatus("待审核");
        activity.setContent(new Scanner(System.in).next());
        String sql = "insert into activity values (null,?,?,?,0)";
        template.update(sql,activity.getActivity(),activity.getContent(),activity.getStatus());
        /*FileWriter fw = new FileWriter("C:\\Users\\啦啦啦\\Desktop\\Activity.txt",true);
        //创建一个对象用来存放活动
        BufferedWriter bw = new BufferedWriter(fw);
        bw.newLine();
        bw.write(activity.getActivity_name()+"/"+activity.getContent()+"/"+"待审核");
        //将活动以活动名/活动内容/审核状态载入
        bw.flush();
        bw.close();
        fw.close();*/
        System.out.println("活动申报成功，请等待管理员审核");
        /*prop.setProperty(activity.getActivity_name(),activity.getContent());
        FileWriter fw = new FileWriter("C:\\Users\\啦啦啦\\Desktop\\Activity.txt",true);
        prop.store(fw,"新活动");
        fw = new FileWriter("C:\\Users\\啦啦啦\\Desktop\\Activity_status.txt",true);
        prop.setProperty(activity.getActivity_name(),"待审核");
        prop.store(fw,"待审核的活动");
        System.out.println("活动申报成功，请等待管理员审核");*/
    }
    //活动审核程序
    public void Activity_enroll(String ACname)//变量是指活动名称
    {
        if (ACname == null) {
            System.out.println("输入有误");
        }
        String sql = "select * from activity";
        //获取表中符合条件的活动,有就返回true,没有就返回false
        List<Activity> list = template.query(sql,new BeanPropertyRowMapper<Activity>(Activity.class));
        int count = 0;
        for (Activity a : list) {
            if(a.getActivity().equals(ACname))
            {
                System.out.println("活动:"+ACname+"\n"+"活动内容:"+a.getContent());
                System.out.println("审核状态:"+a.getStatus());
                if(a.getStatus().equals("待审核"))
                {
                    System.out.println("请选择是否通过该活动");
                    System.out.println("1.通过  2.不通过");
                    switch(new Scanner(System.in).nextInt())
                    {
                        case 1:
                            a.setStatus("审核通过");
                            sql = "update activity set status = ? where activity = ?";
                            count = template.update(sql,a.getStatus(),ACname);
                            break;
                        case 2:
                            a.setStatus("审核不通过");
                            sql = "update activity set status = ? where activity = ?";
                            count = template.update(sql,a.getStatus(),ACname);
                            break;
                    }
                }
                if(count>0)
                {
                    System.out.println("修改成功");
                }
                else
                {
                    System.out.println("修改失败");
                }
                return ;
            }
        }
        System.out.println("未找到活动,请选择输入remove重新寻找或输入任意内容退出");
        if(new Scanner(System.in).next().equals("remove"))
        {
            System.out.println("请重新输入活动名称");
            Activity_enroll(new Scanner(System.in).next());
        }
        /*BufferedReader br = new BufferedReader(new FileReader("C:\\Users\\啦啦啦\\Desktop\\Activity.txt"));
        //创建一个用于修改活动状态的变量,直接覆盖原来的文件
        String line;
        ArrayList<String> txt = new ArrayList<>();
        int i = 0;
        while((line=br.readLine())!=null)
        {
            String[] lines = find_activity(line);
            if(lines[0].equals(ACname))
            {
                i=1;
                System.out.println("活动:"+ACname+"\n"+"活动内容:"+lines[1]);
                System.out.println("审核状态:"+lines[2]);
                if(!(lines[2].equals("审核通过")))
                {
                    System.out.println("请选择是否通过该活动");
                    System.out.println("1.通过  2.不通过");
                    switch(new Scanner(System.in).nextInt())
                    {
                        case 1:lines[2]="审核通过";break;
                        case 2:break;
                    }
                }
                line = ACname+"/"+lines[1]+"/"+lines[2]+"/"+"0";
                //在审核通过状态下,报名人数才设置为0
                //审核没通过,无人报名
                //不用空指针代替是为了防止出现NullPointerException
                //for (int i1 = 1; i1 < lines.length; i1++) {
                    line.concat("/"+lines[i1]);
                }
            }
            txt.add(line);
        }
        if(i==1)
        {
            BufferedWriter bw = new BufferedWriter(new FileWriter("C:\\Users\\啦啦啦\\Desktop\\Activity.txt"));
            for (String s : txt) {
                bw.write(s);
                bw.newLine();
            }
            bw.flush();
        }
        else
        {
            System.out.println("没有找到你想找到的活动,请重新输入活动名称");
            Activity_enroll(new Scanner(System.in).next());
        }
        br.close();*/
       /* Properties prop_find = new Properties();
        prop_find.load(new FileReader("C:\\Users\\啦啦啦\\Desktop\\Activity_status.txt"));
        Set<String> set = prop_find.stringPropertyNames();
        for (String s : set) {
            if(s.equals(ACname))
            {
                if(prop_find.get(s).equals("审核通过"))
                {
                    System.out.println("报名成功");
                }
            }
        }*/
    }

    /*public String[] find_activity(String line)
    {
        return line.split("/");
    }*/
}
